﻿using System;
using System.Collections.Generic;
using UnityEngine;
namespace TinySkill
{

    //技能关键帧枚举类型
    public enum SkillFrameAction
    {
        None,
        LOOP,
        ATTACK,
        BUFF,
        PLAY_SOUND
    }

    //表示单个关键帧数据
    [ProtoBuf.ProtoContract]
    public class config_skill_frame
    {
        //关键帧枚举类型
        [ProtoBuf.ProtoMember(4, IsRequired = true, OverwriteList = true), EditorName("动作"), EditorIgnore]
        public SkillFrameAction action;

        //关键帧开始帧索引
        [ProtoBuf.ProtoMember(5, IsRequired = true, OverwriteList = true)]
        public int frame_index;

        //关键帧持续帧数
        [ProtoBuf.ProtoMember(6, IsRequired = true, OverwriteList = true)]
        public int frame_length = 1;

        //关键帧参数
        [ProtoBuf.ProtoMember(7, IsRequired = true, OverwriteList = true)]
        public config_action data;



        public static Dictionary<SkillFrameAction, Type> action_types = new Dictionary<SkillFrameAction, Type>();
        static config_skill_frame()
        {
            //绑定Unity的内置类型
            var vec3MetaType = ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(Vector3), false);
            vec3MetaType.AddField(1, "x");
            vec3MetaType.AddField(2, "y");
            vec3MetaType.AddField(3, "z");
            var vec2MetaType = ProtoBuf.Meta.RuntimeTypeModel.Default.Add(typeof(Vector2), false);
            vec2MetaType.AddField(1, "x");
            vec2MetaType.AddField(2, "y");

            //关键帧枚举类型与参数类的绑定
            action_types[SkillFrameAction.LOOP] = typeof(config_action_loop);
            action_types[SkillFrameAction.ATTACK] = typeof(config_action_attack);
            action_types[SkillFrameAction.BUFF] = typeof(config_action_buff);
            action_types[SkillFrameAction.PLAY_SOUND] = typeof(config_action_play_sound);
        }

        //通过关键帧枚举类型，创建关键帧参数
        public static config_action Create(SkillFrameAction action)
        {
            if (action_types.ContainsKey(action))
            {
                return (config_action)System.Activator.CreateInstance(action_types[action]);
            }

            return null;
        }
    }

    //关键帧参数基类
    [ProtoBuf.ProtoInclude(1, typeof(config_action_loop))]
    [ProtoBuf.ProtoInclude(2, typeof(config_action_attack))]
    [ProtoBuf.ProtoInclude(3, typeof(config_action_buff))]
    [ProtoBuf.ProtoInclude(4, typeof(config_action_play_sound))]
    [ProtoBuf.ProtoContract]
    public class config_action
    {
    }


    [ProtoBuf.ProtoContract]
    public class config_action_loop : config_action
    {
        [ProtoBuf.ProtoMember(1, IsRequired = true, OverwriteList = true), EditorName("循环次数")]
        public int loop_count = 1;
    }

    //碰撞检测盒
    [ProtoBuf.ProtoContract]
    public class Box
    {
        [ProtoBuf.ProtoMember(1)]
        public Vector3 center;
        [ProtoBuf.ProtoMember(2)]
        public Vector3 size;
    }

    //碰撞检测球
    [ProtoBuf.ProtoContract]
    public class Sphere
    {
        [ProtoBuf.ProtoMember(1)]
        public Vector3 center;
        [ProtoBuf.ProtoMember(2)]
        public float radius;
    }

    //攻击关键帧参数
    [ProtoBuf.ProtoContract]
    public class config_action_attack : config_action
    {
        [ProtoBuf.ProtoMember(1, IsRequired = true, OverwriteList = true), EditorName("攻击盒")]
        public Box[] box;

        [ProtoBuf.ProtoMember(2, IsRequired = true, OverwriteList = true), EditorName("伤害值")]
        public int dmg;
    }

    //添加buff关键帧参数
    [ProtoBuf.ProtoContract]
    public class config_action_buff : config_action
    {
        [ProtoBuf.ProtoMember(1, IsRequired = true, OverwriteList = true), EditorName("范围")]
        public Sphere[] sphere;
    }

    //播放音效关键帧参数
    [ProtoBuf.ProtoContract]
    public class config_action_play_sound : config_action
    {
        [ProtoBuf.ProtoMember(1, IsRequired = true, OverwriteList = true), EditorName("音效路径")]
        public string pathname;
    }
}